﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Data;

using OpenChart.PublicUnits;

namespace OpenChart.DrawChart
{
    class DrawingPieChart:AbsDrawingChart
    {
        private List<PieData> _pieDataList = new List<PieData>();
        private float _angle;//记录当前绘制的扇形角度
        private float _x;//扇形区域左上角横坐标
        private float _y;//扇形区域左上角纵坐标
        private float _width;//扇形区域宽度
        private float _height;//扇形区域高度

        /// <summary>
        /// 绘制数据
        /// </summary>
        protected override void DrawData()
        {
            CalculatePieRegion();
            this.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            foreach (var item in _pieDataList)
            {
                this.DrawPie(this.Graphics,item);
            }
            this.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;
        }

        //绘制扇形
        private void DrawPie(Graphics graphics, PieData item)
        {
            float angle = item.Ratio * 360;
            this.Graphics.FillPie(new SolidBrush(item.Color), _x, _y, _width, _height, _angle, angle);
            _angle += angle;
        }

        /// <summary>
        /// 绘制图例
        /// </summary>
        protected override void DrawLegends()
        {
            int yPosition = CON_UP_RANGE;
            foreach (var item in _pieDataList)
            {
                this.DrawOneLegend(item, yPosition);
                yPosition += 15;
            }
        }

        //绘制一个图例
        private void DrawOneLegend(PieData item, int yPosition)
        {
            this.Graphics.DrawLine(new Pen(item.Color, 10), this.CanvasWidth - CON_RIGTH_RANGE + 10, yPosition, this.CanvasWidth - CON_RIGTH_RANGE + 10 + 35, yPosition);
            this.Graphics.DrawString(item.DataName, new Font("宋体", 12), new SolidBrush(item.Color), this.CanvasWidth - CON_RIGTH_RANGE + 10 + 35 + 10, yPosition - 10);
        }

        /// <summary>
        /// 初始化操作
        /// </summary>
        protected override void Init() 
        {
            //
            //初始化参数Chart所需数据
            //
            double sumData = Convert.ToDouble(this.ChartArea.Data.GetSumValue(this.ChartArea.Series[0].YValueMember));
            foreach (DataRow item in this.ChartArea.Data.Rows)
            {
                PieData pieData = new PieData();
                pieData.DataName = item[this.ChartArea.XValueMember].ToString();
                pieData.Ratio = Convert.ToSingle(Convert.ToDouble(item[this.ChartArea.Series[0].YValueMember]) / sumData);
                Color color = ChartHelper.GetRandomColor();
                while (this.ColorHasExists(color))
                {
                    color = ChartHelper.GetRandomColor();
                }
                pieData.Color = color;
                _pieDataList.Add(pieData);
            }
        }

        //计算扇形区域
        private void CalculatePieRegion()
        {
            //
            //初始化扇形绘制区域
            //
            if (this.CanvasHeight - CON_UP_RANGE - CON_DOWN_RANGE < this.CanvasWidth - CON_LEFT_RANGE - CON_RIGTH_RANGE)
            {
                _width = _height = this.CanvasHeight - CON_UP_RANGE - CON_DOWN_RANGE;
                _y = CON_UP_RANGE;
                _x = (this.CanvasWidth - _width) / 2;
            }
            else
            {
                _width = _height = this.CanvasWidth - CON_LEFT_RANGE - CON_RIGTH_RANGE;
                _x = CON_LEFT_RANGE;
                _y = (this.CanvasHeight - _height) / 2;
            }
        }

        //判断颜色是否已被使用
        private bool ColorHasExists(Color color)
        {
            foreach (var item in _pieDataList)
            {
                if (color == item.Color)
                {
                    return true;
                }
            }
            return false;
        }

        protected override void DrawAxis() { }
        protected override void DrawAxisLable() { }
        protected override void DrawPartingLines(){ }
        protected override void DrawXSacle(){ }
        protected override void DrawYScale(){ }
    }

    struct PieData
    {
        public string DataName { get; set; }

        public float Ratio { get; set; }

        public Color Color { get; set; }
    }
}
